//
//  MvViewController.m
//  YouthOlympic2014
//
//  Created by xiangpaopao on 14-6-30.
//  Copyright (c) 2014年 mitian. All rights reserved.
//

#import "MvViewController.h"
#import <MediaPlayer/MediaPlayer.h>
#import "LandScapeMoviePlayerViewController.h"

@interface MvViewController ()

@property (nonatomic, strong) MPMoviePlayerController *moviePlayer;
@property(nonatomic ,strong) LandScapeMoviePlayerViewController *movieController;
@property (nonatomic) CGRect defaultFrame;

@end

@implementation MvViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self setTitle:@"青奥会宣传片"];
    
    NSURL *url = [NSURL URLWithString:@"http://www.nanjing2013.org/upload/flv/yaqing.mp4"];

    self.moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:url];
    self.moviePlayer.allowsAirPlay = YES;
    [self.moviePlayer.view setFrame:CGRectMake(0, ScreenHeight/2 -170, 320, 210)];
    [self.moviePlayer setControlStyle:MPMovieControlStyleNone];
    [self.view addSubview:self.moviePlayer.view];
    
    
    [self.moviePlayer play];
    [self.playBtn setSelected:YES];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(updateSlider) userInfo:nil repeats:YES];
}

- (void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
    [self.playBtn setSelected:NO];
    [self.moviePlayer stop];
    
}

- (void)updateSlider // triggered by timer
{
    float currentPlaybackRatio;
    if ([self.moviePlayer currentPlaybackTime] > 0)
    {
        currentPlaybackRatio = [self.moviePlayer currentPlaybackTime] / [self.moviePlayer duration];
    }
    else currentPlaybackRatio = 0;
    
    [self.progressBar setProgress:currentPlaybackRatio animated:YES];
}


- (IBAction)soundAction:(id)sender
{
    NSLog(@"%f",MPMusicPlayerController.applicationMusicPlayer.volume);
    
    if (MPMusicPlayerController.applicationMusicPlayer.volume!=0.0) {
        [[MPMusicPlayerController applicationMusicPlayer] setVolume:0.0];
        [self.soundBtn setSelected:YES];
    }else{
        [[MPMusicPlayerController applicationMusicPlayer] setVolume:1.0];
        [self.soundBtn setSelected:NO];
    }
    
}
- (IBAction)playAction:(id)sender
{
    if (self.moviePlayer.playbackState!=MPMoviePlaybackStatePlaying) {
        [self.playBtn setSelected:YES];
        [self.moviePlayer play];
        
    }else if(self.moviePlayer.playbackState==MPMoviePlaybackStatePlaying)
    {
        [self.playBtn setSelected:NO];
        [self.moviePlayer pause];
    }
}

- (IBAction)screenAction:(id)sender
{
    [self.playBtn setSelected:NO];
    [self.moviePlayer pause];
    
    //[self.moviePlayer setFullscreen:YES];
    NSURL *url = [NSURL URLWithString:@"http://www.nanjing2013.org/upload/flv/yaqing.mp4"];
    self.movieController = [[LandScapeMoviePlayerViewController alloc] initWithContentURL:url];
    self.movieController.moviePlayer.initialPlaybackTime = self.moviePlayer.currentPlaybackTime;
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(movieFinishedCallback:)
                                               name:MPMoviePlayerPlaybackDidFinishNotification
                                             object:self.movieController.moviePlayer];
    [self presentMoviePlayerViewControllerAnimated:self.movieController];
}

-(void)movieFinishedCallback:(NSNotification*)notify{
    NSLog(@"movieFinishedCallback");
    [self.moviePlayer setCurrentPlaybackTime:self.movieController.moviePlayer.currentPlaybackTime];
    [self.playBtn setSelected:YES];
    [self.moviePlayer play];
}


@end
